//
//  SendTieziViewController.m
//  shusheng
//
//  Created by rimi on 16/7/18.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "SendTieziViewController.h"
#import "AVFile+UploadMoreFile.h"

@interface SendTieziViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

{
    UIImagePickerController *picker;
    NSMutableArray *imnageArr;
    NSMutableArray *_bigImagefileArr;
}
- (void)initializeUserInterface; /** < 初始化用户界面 */
@property (nonatomic,strong) UILabel *titleName; // 主题
@property (nonatomic,strong) UITextField *titleTextField; // 主题输入框
@property (nonatomic,strong) UILabel *contentName; // 内容
@property (nonatomic,strong) UITextView *contentTextField; // 内容输入框
@property (nonatomic,strong) UIView *imageContent; // 显示三张图片
@property (nonatomic,strong) UIButton *addImages; // 添加图片；

@property (nonatomic,strong) UIButton *sendButton;

@end

@implementation SendTieziViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    imnageArr = [[NSMutableArray alloc] init];
    _bigImagefileArr = [[NSMutableArray alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,80, 45)];
    [backButton setTitle:@"<返回" forState:UIControlStateNormal];
    [backButton setTitleColor:FONT_COLOR forState:UIControlStateNormal];
    
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.center = CGPointMake( 120, 32);
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(BUTTON_HEIGHT);
        make.left.equalTo(self.view).with.offset(10);
        make.width.offset(50);
        make.height.offset(30);
    }];

    [self initializeUserInterface];
}


- (void)initializeUserInterface{
    [self.view addSubview:self.titleName];
    [self.titleName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.left.equalTo(self.view).with.offset(50);
        make.width.mas_equalTo(SCREEN_WIDTH*0.15);
        make.height.mas_equalTo(40);
    }];
    
    [self.view addSubview:self.titleTextField];
    [self.titleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.left.mas_equalTo(self.titleName.mas_right).with.offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH*0.5);
        make.height.mas_equalTo(40);
    }];
    
    [self.view addSubview:self.contentName];
    [self.contentName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleName.mas_bottom).with.offset(20);
        make.left.equalTo(self.view).with.offset(50);
        make.width.mas_equalTo(SCREEN_WIDTH*0.15);
        make.height.mas_equalTo(40);
    }];
    
    [self.view addSubview:self.contentTextField];
    [self.contentTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleTextField.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self.contentName.mas_right).with.offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH*0.5);
        make.height.mas_equalTo(SCREEN_HEIGHT * 0.15);
    }];
    
    [self.view addSubview:self.imageContent];
    [self.imageContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentTextField.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self.contentName.mas_right).with.offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH*0.5);
        make.height.mas_equalTo(SCREEN_WIDTH*0.5/3);
    }];
    [self.view addSubview:self.addImages];
    [self.addImages mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imageContent.mas_right).with.offset(10);
        make.right.mas_equalTo(self.view).with.offset(-10);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(self.imageContent.mas_top).with.offset(10);
    }];
    
    [self.view addSubview:self.sendButton];
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageContent.mas_bottom).with.offset(20);
        make.height.mas_equalTo(45);
        make.width.mas_equalTo(SCREEN_WIDTH *0.5);
        make.centerX.mas_equalTo(self.view);
    }];
   
}

- (void)backButton:sender{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}


#pragma mark --  Getters
- (UIButton *)addImages{
    if (!_addImages) {
        _addImages = [[UIButton alloc] init];
        [_addImages setTitle:@"添加图片" forState:UIControlStateNormal];
        [_addImages addTarget:self action:@selector(respondsAddImages:) forControlEvents:UIControlEventTouchUpInside];
        _addImages.backgroundColor = [UIColor orangeColor];
    }
    return _addImages;
}

- (void)respondsAddImages:(UIButton *)sender{
    [self getImages];
}

- (UILabel *)titleName{
    if (!_titleName) {
        _titleName = [[UILabel alloc] init];
        _titleName.text = @"主题：";
        _titleName.textAlignment = NSTextAlignmentRight;
//        _titleName.backgroundColor = [UIColor greenColor];
    }
    return _titleName;
}

- (UITextField *)titleTextField{
    if (!_titleTextField) {
        _titleTextField = [[UITextField alloc] init];
        _titleTextField.borderStyle = UITextBorderStyleLine;
//        _titleTextField.backgroundColor = [UIColor orangeColor];
    }
    return _titleTextField;
}

- (UILabel *)contentName{
    if (!_contentName) {
        _contentName = [[UILabel alloc] init];
        _contentName.text = @"描述：";
        _contentName.textAlignment = NSTextAlignmentRight;
//        _contentName.backgroundColor = [UIColor greenColor];
    }
    return _contentName;
}

-(UITextView *)contentTextField{
    if (!_contentTextField) {
        _contentTextField = [[UITextView alloc] init];
        _contentTextField.font = [UIFont systemFontOfSize:17];
        _contentTextField.layer.borderColor = [UIColor grayColor].CGColor;
        _contentTextField.layer.borderWidth =1.0;
        _contentTextField.layer.cornerRadius =5.0;
        
        
    }
    return _contentTextField;
}


- (UIView *)imageContent{
    if (!_imageContent) {
        _imageContent = [[UIView alloc] init];
        _imageContent.layer.borderWidth = 1;
//        _imageContent.backgroundColor = [UIColor redColor];
    }
    return _imageContent;
}

- (UIButton *)sendButton{
    if (!_sendButton) {
        _sendButton = [[UIButton alloc] init];
        [_sendButton setTitle:@"发布" forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(respondsToSendButton:) forControlEvents:UIControlEventTouchUpInside];
        
        _sendButton.backgroundColor = [UIColor orangeColor];
    }
    return _sendButton;
}

- (void)respondsToSendButton:(UIButton *)sender{
   
//    NSMutableArray *imageArr = [[NSMutableArray alloc] init];
    if (self.titleTextField.text.length == 0) {
        [MBProgressHUD showError:@"主题不能为空"];
    } else{
        AVObject *Tiezi = [[AVObject alloc] initWithClassName:@"Tiezi"];// 构建对象
        [Tiezi setObject:self.titleTextField.text forKey:@"title"];// 设置名称
        [Tiezi setObject:self.contentTextField.text forKey:@"content"];
        [Tiezi setObject:[AVUser currentUser] forKey:@"author"];
        [Tiezi setObject:@1 forKey:@"priority"];// 设置优先级
        [Tiezi saveInBackground];// 保存到云端
        
//        AVFile *imageFile = [AVFile fileWithAVObject:Tiezi];
        
        
        for (int i = 0; i < imnageArr.count; i ++) {
            UIImage * image = imnageArr[i];
            NSData * data = UIImagePNGRepresentation(image);
            AVFile * imageArr = [AVFile fileWithName:[NSString stringWithFormat:@"image%d",i] data:data];
            [_bigImagefileArr addObject:imageArr];
        }
        [AVFile uploadFiles:_bigImagefileArr completion:^(NSString *fileUrlString, NSError *error) {
            NSArray *urlarr = [[NSArray alloc]init];
            urlarr = [fileUrlString componentsSeparatedByString:@","];
            [Tiezi setObject:urlarr forKey:@"images"];
            [Tiezi saveInBackground];
            NSLog(@"%@",urlarr);
        }];

      
        [MBProgressHUD showSuccess:@"发布成功"];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        NSLog(@"发布");
    }
   
}


- (void)getImages{
    picker = [[UIImagePickerController alloc]init];
    picker.view.backgroundColor = [UIColor orangeColor];
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.sourceType = sourcheType;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [imnageArr addObject:image];
    NSLog(@"%@",imnageArr);
    for (int i = 0; i < imnageArr.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (SCREEN_WIDTH*0.5/3), 0, SCREEN_WIDTH*0.5/3, SCREEN_WIDTH*0.5/3)];
        [self.imageContent addSubview:imageView];
        imageView.image = imnageArr[i];
    }
    [self dismissViewControllerAnimated:picker completion:nil];
    
}




@end
